<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <div>
                <el-button v-perms="['area:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table class="mt-4" size="large" :data="lists" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
                <el-table-column label="地区名称" prop="name" min-width="100" />
                <el-table-column label="地区级别" prop="level" min-width="100" >
                    <template #default="{ row }">
                        <dict-value :options="dictData.area_level" :value="row.level" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['area:edit']" type="primary" link @click="handleAdd(row.id)">
                            新增
                        </el-button>
                        <el-button v-perms="['area:edit']" type="primary" link @click="handleEdit(row.id)">
                            编辑
                        </el-button>
                        <el-button v-perms="['area:del']" type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="area">
import { areaDelete, areaLists } from '@/api/area'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {menuLists} from "@/api/perms/menu";
import {useDictData} from "@/hooks/useDictOptions";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: '',
    pid: '',
    level: '',
})
const loading = ref(false)
const lists = ref([])

const { dictData } = useDictData<{
    area_level: any[],
}>(['area_level'])

const getLists = async () => {
    loading.value = true
    try {
        const data = await areaLists()
        lists.value = data
        loading.value = false
    } catch (error) {
        loading.value = false
    }
}

const handleAdd = async (id?: number) => {
    showEdit.value = true
    await nextTick()
    if (id) {
        editRef.value?.setFormData({
            pid: id
        })
    }
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await areaDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
